//
//  WithDrawCashViewController.m
//  MyNewProject
//
//  Created by apple on 2019/3/12.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "WithDrawCashViewController.h"
#import "WithDrawViewCell.h"
#import "CashDetailViewController.h"

@interface WithDrawCashViewController ()<UITableViewDelegate
,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic , retain)UITableView * tableList;
@property(nonatomic , retain)UIButton * toSaleSquare;//去卖场

@property(nonatomic , retain)NSArray * tipArray;//去卖场
@property(nonatomic , retain)NSArray * placeholderArray;//去卖场

@property(nonatomic , retain)UILabel * lab;
@end

@implementation WithDrawCashViewController
-(NSArray *)tipArray{
    if (!_tipArray) {
        _tipArray = @[@[@"提现金额"],@[@"姓名",@"提现账号"]];
    }
    return _tipArray;
}
-(NSArray *)placeholderArray{
    if (!_placeholderArray) {
        _placeholderArray = @[@[@"请输入提现金额"],@[@"请输入您的姓名",@"请输入您的支付宝账号"]];
    }
    return _placeholderArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navigationBar.hidden = NO;
    self.gk_navTitle = @"提现";
    [self.view addSubview:self.tableList];
    [self.view addSubview:self.toSaleSquare];
    UIButton  * customBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
    [customBtn setTitle:@"提现记录" forState:UIControlStateNormal];
    [customBtn setTitleColor:SKColorWithHex(0x7ab801) forState:UIControlStateNormal];
    customBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
    [customBtn addTarget:self action:@selector(recordBtn) forControlEvents:UIControlEventTouchUpInside];
    self.gk_navRightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customBtn];
}
-(void)recordBtn{
//    [SVProgressHUD showSuccessWithStatus:@"提现记录"];
    
    CashDetailViewController  * cashVC = [CashDetailViewController new];
    
    [self.navigationController pushViewController:cashVC animated:YES];
    
    
}
#pragma -makr Setter
-(UITableView *)tableList{
    if (!_tableList) {
        _tableList = [[UITableView alloc]initWithFrame:CGRectMake(0, IS_IPhoneX_All?88:64, SCREEN_WIDTH, SCREENH_HEIGHT  - 50 - SKBottomSafe_Height - (IS_IPhoneX_All?88:64))  style:UITableViewStylePlain];
        _tableList.dataSource = self;
        _tableList.delegate = self;
        _tableList.showsVerticalScrollIndicator = NO;
        _tableList.showsHorizontalScrollIndicator = NO;
        _tableList.separatorStyle = UITableViewCellSelectionStyleNone;
        [_tableList addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keboardDismiss)]];
    }
    return  _tableList;
}
-(UIButton *)toSaleSquare{
    if (!_toSaleSquare) {
        _toSaleSquare = [UIButton buttonWithType:UIButtonTypeCustom];
        _toSaleSquare.frame = CGRectMake(0, SCREENH_HEIGHT - 50 - SKBottomSafe_Height, SCREEN_WIDTH, 50);
        _toSaleSquare.backgroundColor = [UIColor grayColor];
        [_toSaleSquare setTitle:@"立即提现" forState:UIControlStateNormal];
        [_toSaleSquare setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_toSaleSquare addTarget:self action:@selector(toSaleSquarePress) forControlEvents:UIControlEventTouchUpInside];
        _toSaleSquare.backgroundColor = SKColorWithHex(0x7ab801);
    }
    return _toSaleSquare;
}
-(void)toSaleSquarePress{
    WithDrawViewCell *cell = (WithDrawViewCell *)[self.tableList cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (IsStrEmpty(cell.contentTextField.text)) {
        [self.view makeToast:@"请输入提现金额" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    
    WithDrawViewCell *cell1 = (WithDrawViewCell *)[self.tableList cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
    if (IsStrEmpty(cell1.contentTextField.text)) {
        [self.view makeToast:@"请输入您的支付宝账号" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    
    [YQNetworking postWithUrl:@"Pay/tixian" refreshRequest:YES cache:NO params:@{@"uid":self.uid,@"loginKey":self.loginKey,@"money":cell.contentTextField.text,@"bank_name":SafeString([UserInfoSingleton shareInstance].userInfo.name),@"bank_card":cell1.contentTextField.text} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
        
    } successBlock:^(id response) {

        if ([response[@"status"] isEqualToString:@"ok"]) {
            SKWeakSelf;
            [self.view makeToast:@"需要后台审核，24小时内到账" duration:1 position:CSToastPositionCenter];
            self.lab.text = [NSString stringWithFormat:@"可提现:%.2f元",[UserInfoSingleton shareInstance].userInfo.keyong_yue.doubleValue -[cell.contentTextField.text doubleValue]];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                CashDetailViewController  * cashVC = [CashDetailViewController new];

                [weakSelf.navigationController pushViewController:cashVC animated:YES];
            });
        }
        else
        {
            [self.view makeToast:SafeString(response[@"msg"]) duration:1 position:CSToastPositionCenter];

        }

    } failBlock:^(NSError *error) {
        
    }];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;//商品简介 竞购成功者 商品参数  商品图片
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.tipArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WithDrawViewCell";
    WithDrawViewCell *cell = (WithDrawViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
    if (cell == nil) {
        cell = [[WithDrawViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.tipLab.text = self.tipArray[indexPath.section][indexPath.row];
    cell.contentTextField.placeholder = self.placeholderArray[indexPath.section][indexPath.row];
    cell.contentTextField.delegate = self;
    cell.contentTextField.keyboardType = indexPath.section == 0?UIKeyboardTypeDecimalPad:UIKeyboardTypeDefault;
    if (indexPath.section == 1 && indexPath.row == 0) {//姓名 已实名认证保持一致 不可修改
        cell.contentTextField.userInteractionEnabled = NO;
        cell.contentTextField.text = SafeString([UserInfoSingleton shareInstance].userInfo.name);
        cell.contentTextField.clearButtonMode = UITextFieldViewModeNever;
    }else{
        cell.contentTextField.userInteractionEnabled = YES;
        cell.contentTextField.text = @"";
        cell.contentTextField.clearButtonMode = UITextFieldViewModeAlways;
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, section == 0?30:10)];
    if (section == 0) {
        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 30)];
        lab.font = LightFont(13);
        lab.textColor = [UIColor darkGrayColor];
        lab.text = [NSString stringWithFormat:@"可提现:%.2f元",[UserInfoSingleton shareInstance].userInfo.keyong_yue.doubleValue];
        self.lab = lab;
        [view addSubview:lab];
    }
    view.backgroundColor = BackColor;
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0?30:10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
-(void)keboardDismiss{
    [self.view endEditing:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    
}
@end
